//
//  StageView.m
//  NewGame
//
//  Created by wwj on 15/9/24.
//  Copyright © 2015年 wwj. All rights reserved.
//

#import "StageView.h"
#import "SoundTool.h"
#import "StageModel.h"
#import "StageRecordModel.h"
#import "StageRecordTool.h"

@interface StageView ()
@property (weak, nonatomic) IBOutlet UIButton *numberLabel;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak ,nonatomic) IBOutlet UIView *stageLock;
@property (weak, nonatomic) IBOutlet UIView *coverView;
@property (weak, nonatomic) IBOutlet UIImageView *stageBorder;
@property (weak, nonatomic) IBOutlet UIImageView *stageNew;
@property (weak, nonatomic) IBOutlet UIImageView *stageShadow;
@property (weak, nonatomic) IBOutlet UIImageView *stageRank;

@end

@implementation StageView

+(instancetype)stageView{
    return [[[NSBundle mainBundle] loadNibNamed:@"StageView" owner:nil options:nil] lastObject];
}

-(void)awakeFromNib{
    UIImage *image1= [UIImage imageNamed:@"select_stage_s01"];
    UIImage *image2= [UIImage imageNamed:@"select_stage_s02"];
    UIImage *image3= [UIImage imageNamed:@"select_stage_s03"];
    _stageBorder.animationImages = @[image1, image2, image3];
    _stageBorder.animationDuration = 0.3;
}

-(void)setModel:(StageModel *)model{
    _model = model;
    
    [_numberLabel setTitle:[NSString stringWithFormat:@"%d", model.no] forState:UIControlStateNormal];
    
    _imageView.image = [UIImage imageNamed:model.icon];
    [self updateStageState];
}



/**
 *  更新关卡数据
 */
-(void)updateStageState{
    if (_model.recordModel) {
        //有记录
        if (_model.recordModel.isUnlocked) {
            //已经解锁
            [self updateWithUnlocked];
        }else{
            //即将解锁
            [self updateWithWillUnlocked];
        }
    }else{
        //没有记录
        [self updateWithNoRecord];
    }
}

-(void)updateWithUnlocked{
    
    [_stageLock removeFromSuperview];
    [_coverView removeFromSuperview];
    self.userInteractionEnabled = YES;
    if (_model.recordModel.rank) {
        //玩过
        _stageNew.hidden = YES;
        _stageRank.hidden = NO;
        _stageShadow.hidden = NO;
        NSString *name = [NSString stringWithFormat:@"select_stage_%@.png", _model.recordModel.rank];
        _stageRank.image = [UIImage imageNamed:name];
        if ([@"s" isEqualToString:_model.recordModel.rank]) {
            _stageBorder.image = [UIImage imageNamed:@"select_stage_normal"];
            
            [_stageBorder startAnimating];
        }else{
            //显示白框
            _stageBorder.image = [UIImage imageNamed:@"select_stage_new"];
        }
    }else{
        //没有玩过
        _stageNew.hidden = NO;
        //显示白框
        _stageBorder.image = [UIImage imageNamed:@"select_stage_new"];
    }

}

-(void)updateWithWillUnlocked{
    
    self.userInteractionEnabled = NO;
    
    //添加动画
    CAAnimationGroup *group = [self sharkAndScaleAnimation];
    group.delegate = self;
    [self.layer addAnimation:group forKey:nil];
    
    //添加音效
    [[SoundTool sharedSoundTool] playSound:kSoundUnLock];
    
    //移除遮盖
    [_coverView removeFromSuperview];
    
    //设置属性已经解锁
    _model.recordModel.unlocked = YES;
    [[StageRecordTool sharedStageRecordTool] saveStageRecord:_model.recordModel];
    
}

-(void)updateWithNoRecord{
    self.userInteractionEnabled = NO;
}


-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    CGFloat destY = [UIScreen mainScreen].bounds.size.height - self.frame.origin.y;
    CGFloat duration = destY / 1000;
    [UIView animateWithDuration:duration animations:^{
        CGRect frame = _stageLock.frame;
        frame.origin.y = destY;
        _stageLock.frame = frame;
        
    } completion:^(BOOL finished) {
        
        self.userInteractionEnabled = YES;
        
        [_stageLock removeFromSuperview];
        
        //添加音效
        [[SoundTool sharedSoundTool] playSound:kSoundNew];
        
        //添加动画
        CAAnimationGroup *group = [self sharkAndScaleAnimation];
        [_stageNew.layer addAnimation:group forKey:nil];
        
        //显示白框
        _stageBorder.image = [UIImage imageNamed:@"select_stage_new"];
        _stageNew.hidden = NO;
        
    }];
}

-(CAAnimationGroup *)sharkAndScaleAnimation{
    //摇摆动画
    CAKeyframeAnimation *shark = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
    CGFloat angel = 0.4;
    shark.values = @[@(-angel), @(angel-0.1), @(-angel)];
    shark.duration = 0.2;
    shark.repeatCount = 2;
    
    //缩放动画
    CAKeyframeAnimation *scale = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    scale.values  = @[@1.2, @1.0];
    
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations =@[shark, scale];
    
    return  group;
}

@end
